import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface User {
  id: string
  username: string
  name: string
  avatar?: string
}

export const useAuthStore = defineStore('auth', () => {
  const user = ref<User | null>(null)
  const isLoggedIn = ref(false)
  const token = ref<string | null>(null)

  // 登录
  const login = (userData: User, userToken?: string) => {
    user.value = userData
    isLoggedIn.value = true
    token.value = userToken || 'mock-token'
  }

  // 登出
  const logout = () => {
    user.value = null
    isLoggedIn.value = false
    token.value = null
  }

  // 检查登录状态
  const checkAuth = () => {
    return isLoggedIn.value && user.value !== null
  }

  // 获取用户信息
  const getUserInfo = () => {
    return user.value
  }

  return {
    user,
    isLoggedIn,
    token,
    login,
    logout,
    checkAuth,
    getUserInfo
  }
}, {
  persist: {
    key: 'auth-store',
    storage: localStorage,
    paths: ['user', 'isLoggedIn', 'token']
  }
})